package com.lefeng.cockpit.datax.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lefeng.cockpit.common.bean.po.datasource.JobRegistryPO;
import com.lefeng.cockpit.datax.dao.JobRegistryDao;
import com.lefeng.cockpit.datax.dao.mapper.JobRegistryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JobRegistryDaoImpl implements JobRegistryDao {

    private static final String REGISTRY_GROUP = "registry_group";
    private static final String REGISTRY_KEY = "registry_key";
    private static final String REGISTRY_VALUE = "registry_value";


    @Resource
    private JobRegistryMapper jobRegistryMapper;

    @Override
    public int registryUpdate(JobRegistryPO jobRegistryPO) {
        QueryWrapper<JobRegistryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(REGISTRY_GROUP, jobRegistryPO.getRegistryGroup())
                .eq(REGISTRY_KEY, jobRegistryPO.getRegistryKey())
                .eq(REGISTRY_VALUE, jobRegistryPO.getRegistryValue());
        return jobRegistryMapper.update(jobRegistryPO, queryWrapper);
    }

    @Override
    public int registrySave(JobRegistryPO jobRegistryPO) {
        return jobRegistryMapper.insert(jobRegistryPO);
    }

    @Override
    public int registryDelete(JobRegistryPO jobRegistryPO) {
        QueryWrapper<JobRegistryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(REGISTRY_GROUP, jobRegistryPO.getRegistryGroup())
                .eq(REGISTRY_KEY, jobRegistryPO.getRegistryKey())
                .eq(REGISTRY_VALUE, jobRegistryPO.getRegistryValue());
        return jobRegistryMapper.delete(queryWrapper);
    }

    @Override
    public List<JobRegistryPO> findAll(int deadTimeout, Date date) {
        LambdaQueryWrapper<JobRegistryPO> queryWrapper = new LambdaQueryWrapper<>();

        // Assuming `updateTime` is of type LocalDateTime
        LocalDateTime thresholdTime = LocalDateTime.now().minusSeconds(deadTimeout);

        queryWrapper.gt(JobRegistryPO::getUpdateTime, thresholdTime);

        return jobRegistryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Integer> findDead(int deadTimeout, Date date) {
        LambdaQueryWrapper<JobRegistryPO> queryWrapper = new LambdaQueryWrapper<>();

        // Assuming `updateTime` is of type LocalDateTime
        LocalDateTime thresholdTime = LocalDateTime.now().minusSeconds(deadTimeout);

        queryWrapper.lt(JobRegistryPO::getUpdateTime, thresholdTime);

        return jobRegistryMapper.selectList(queryWrapper)
                .stream()
                .map(JobRegistryPO::getId)
                .collect(Collectors.toList());
    }

    @Override
    public void removeDead(List<Integer> ids) {
        jobRegistryMapper.deleteBatchIds(ids);
    }
}
